﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Baza.EF.BLL;
using Baza.EF.DAL;

namespace Pep_3014_3070.Owners
{
    public partial class ProfilePage : System.Web.UI.Page
    {
        protected int OwnerID
        {
            get { return (int)ViewState["OwnerID"]; }
            set { ViewState["OwnerID"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            OwnerID = Convert.ToInt32(RouteData.Values["id"]);
            BindData();
        }

        private void BindData()
        {
            Baza.EF.DAL.Owners tempC = DAOwners.getOwnerByID(Convert.ToInt32(OwnerID));

            FirstNameInput.Text = tempC.FirstName;
            LastNameInput.Text = tempC.LastName;
            EmailInput.Text = tempC.Email;
            Address1Input.Text = tempC.Address.Address1;
            Address2Input.Text = tempC.Address.Address2;
            StreetNumberInput.Text = tempC.Address.StreetNumber.ToString();
            CityInput.Text = tempC.Address.Cities.Name;
            CountryDropDownInput.Text = tempC.Address.Cities.Countries.Country_name;
            CellNumberInput.Text = tempC.CellNumber;
            BusinessNumberInput.Text = tempC.BusinessNumber;
        }

        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            Baza.EF.DAL.Owners o = new Baza.EF.DAL.Owners();

            o.FirstName = FirstNameInput.Text;
            o.LastName = LastNameInput.Text;
            o.HomeNumber = HomeNumberInput.Text;
            o.WebsiteURL = WebsiteURLInput.Text;
            o.FaxNumber = FaxNumberInput.Text;
            o.Email = EmailInput.Text;
            o.CellNumber = CellNumberInput.Text;
            o.BusinessNumber = BusinessNumberInput.Text;
            o.AdditionalContactInfo = BusinessNumberInput.Text;

            if (PasswordInput.Text != "")
            {
                o.PasswordSalt = UIHelper.GenerateSalt();
                o.PasswordHash = UIHelper.GenerateHash(PasswordInput.Text, o.PasswordSalt);
            }
            DAOwners.UpdateProfilePage(o);
        }
    }
}